package cn.itcast.usermanage.controller;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.usermanage.pojo.PageResult;
import cn.itcast.usermanage.pojo.PageStatusResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Controller
@RequestMapping("/user")
public class RestUserController {

	@Autowired
	private UserService userService;

	@GetMapping
	@ResponseBody
	public ResponseEntity<PageResult<User>> queryUserListBypage(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "5") Integer rows) {
		try {
			PageResult<User> result = userService.queryUserListBypage(page, rows);
			if (result != null && result.getTotal() != 0) {
				// 资源存在返回200
				return ResponseEntity.ok(result);
			}
			// 资源不存在返回404
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		} catch (Exception e) {
			e.printStackTrace();
			// 出现异常返回500
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@PostMapping
	public ResponseEntity<Void> saveUser(User user) {
		try {
			userService.saveUser(user);
			return new ResponseEntity<Void>(HttpStatus.CREATED);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@DeleteMapping
	public ResponseEntity<Void> deleteUser(@RequestParam("ids") Long[] ids) {
		try {
			userService.deleteUserByIds(ids);
			// 成功，返回204 ,操作成功，但是不返回数据
			return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	@PutMapping
	public ResponseEntity<Void> updateUser(User user){
		try {
			// 修改用户
			this.userService.updateUser(user);
			// 成功，返回204 ,操作成功，但是不返回数据
			return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
		} catch (Exception e) {
			e.printStackTrace();
			// 出现异常，返回500
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	@RequestMapping("/export/excel")
	public ModelAndView exportExcel(@RequestParam(value = "page") Integer page,
			@RequestParam(value = "rows") Integer rows) {
		ModelAndView mv = new ModelAndView("excelView");
		// 查询分页信息
		PageResult<User> result = userService.queryUserListBypage(page, rows);
		mv.addObject("users", result.getRows());
		return mv;
	}

	

	@GetMapping("{id}")
	public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
		try {
			User user = userService.queryUserById(id);
			if (user != null) {
				// 资源存在返回200
				return ResponseEntity.ok(user);
			}
			// 资源不存在返回404
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		} catch (Exception e) {
			e.printStackTrace();
			// 出现异常返回500
			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}
